__all__ = (
    "MyABE",
)

from .cpABE import ABE
from binascii import a2b_hex,b2a_hex

class MyABE:
    __core = ABE()

    @classmethod
    def setup(cls)->(str,str):
        public_key_bytes, master_key_bytes = cls.__core.setup()
        public_key:str = b2a_hex(public_key_bytes).decode("utf-8")
        master_key:str = b2a_hex(master_key_bytes).decode("utf-8")
        return public_key,master_key

    @classmethod
    def gen_user_priv_key(cls,public_key:str,master_key:str,user_attrs_list:list)->str:
        public_key_bytes:bytes = a2b_hex(public_key)
        master_key_bytes:bytes = a2b_hex(master_key)
        private_key_bytes:bytes = cls.__core.keygen(public_key_bytes,master_key_bytes,user_attrs_list)
        private_key:str = b2a_hex(private_key_bytes).decode("utf-8")
        return private_key 

    @classmethod
    def encrypt(cls, public_key:str, msg:str, access_policy:list)->str:
        """
            access_policy:
            [[a,b],[c,d],e]  ==> (a and b) or (c and d) or e
        """
        public_key_bytes:bytes = a2b_hex(public_key)
        cipher_bytes:bytes = cls.__core.encrypt(public_key_bytes,msg,access_policy)
        ciphertext:str = b2a_hex(cipher_bytes).decode("utf-8")
        return ciphertext

    @classmethod
    def decrypt(cls, public_key:str, private_key:str, ciphertext:str)->str:
        public_key_bytes:bytes = a2b_hex(public_key)
        private_key_bytes:bytes = a2b_hex(private_key)
        cipher_bytes:bytes = a2b_hex(ciphertext)
        msg:str = cls.__core.decrypt(public_key_bytes,private_key_bytes,cipher_bytes)
        return msg





